/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.wx.startup;

import cn.ac.iie.di.wx.common.RuntimeEnv;
import cn.ac.iie.di.wx.utils.DateUtil;
import cn.ac.iie.di.wx.configuration.Configuration;
import cn.ac.iie.di.wx.httpservice.HttpGetter;
import cn.ac.iie.di.wx.utils.RedisUtil;
import org.apache.log4j.Logger;

/**
 * 部署节点是ycl@10.136.16.29
 *
 * @author Administrator
 */
public class Starter {

    static Logger logger = Logger.getLogger(Starter.class.getName());

    public static void main(String[] args) {
        try {
            init();
            HttpGetter httpGetter = new HttpGetter();

            String redisHosts = ((String) RuntimeEnv.getParam(RuntimeEnv.REDIS_HOSTS));
            String masterName = (String) RuntimeEnv.getParam(RuntimeEnv.REDIS_MASTER_NAME);
            String dbName = (String) RuntimeEnv.getParam(RuntimeEnv.REDIS_DB_NAME);

            String user = (String) RuntimeEnv.getParam(RuntimeEnv.USER);
            String password = (String) RuntimeEnv.getParam(RuntimeEnv.PASSWORD);
            String yesterdayDate = DateUtil.getYesterdayDate();

            RedisUtil userRedisUtil = new RedisUtil(redisHosts, masterName, Integer.parseInt(dbName));
            logger.info("init redis:"+redisHosts+"\t"+masterName+"\t"+dbName+"\tuse:"+user+"-"+password+"at "+yesterdayDate);
            String yesterdayDataSize = userRedisUtil.get((String) RuntimeEnv.getParam(RuntimeEnv.DATASIZE) + yesterdayDate);//dataSize
            String theDayBeforeDataSize = userRedisUtil.get((String) RuntimeEnv.getParam(RuntimeEnv.DATASIZE) + DateUtil.getTheDayBeforeDate());
            String allCount = userRedisUtil.get((String) RuntimeEnv.getParam(RuntimeEnv.ALLCOUNT) + yesterdayDate);
            String successCount = userRedisUtil.get((String) RuntimeEnv.getParam(RuntimeEnv.SUCCESSCOUNT) + yesterdayDate);

            // 当天用户和昨天用户信息是否相同的一个估量， 0：不同; 10：相同,用当天的datasize减去昨天的
            String evaluate;
            logger.info((String) RuntimeEnv.getParam(RuntimeEnv.DATASIZE) + yesterdayDate+"="+yesterdayDataSize);
            logger.info((String) RuntimeEnv.getParam(RuntimeEnv.DATASIZE) + DateUtil.getTheDayBeforeDate()+"="+theDayBeforeDataSize);
            logger.info((String) RuntimeEnv.getParam(RuntimeEnv.ALLCOUNT) + yesterdayDate+"="+allCount);
            logger.info((String) RuntimeEnv.getParam(RuntimeEnv.SUCCESSCOUNT) + yesterdayDate+"="+successCount);
            if (yesterdayDataSize != null && allCount != null && successCount != null && theDayBeforeDataSize != null) {
                if (yesterdayDataSize.equals(theDayBeforeDataSize)) {
                    evaluate = "10";
                } else {
                    evaluate = "0";
                }
                logger.info("yesterdayDataSize=" + yesterdayDataSize + "theDayBeforeDataSize=" + theDayBeforeDataSize + "allCount=" + allCount + "successCount=" + successCount + "evaluate=" + evaluate);
                String userrequest = "user=" + user + "&password=" + password + "&date=" + yesterdayDate
                        + "&dataSize=" + yesterdayDataSize + "&allCount=" + allCount + "&successCount=" + successCount + "&evaluate=" + evaluate;
                String user_url = (String) RuntimeEnv.getParam(RuntimeEnv.USER_URL) + "?";

                httpGetter.doGet(user_url + userrequest);
            } else {
                logger.warn("can not get user datas from redis");
            }

        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }
    }

    public static void init() throws Exception {
        String configurationFileName = "serverport.properties";
        Configuration conf = Configuration.getConfiguration(ClassLoader.getSystemClassLoader().getResource(configurationFileName).getPath());
        if (conf == null) {
            throw new Exception("reading " + configurationFileName + " is failed.");
        }
        try {
            RuntimeEnv.initialize(conf);///参数，
        } catch (Exception ex) {
            throw new Exception("initializng runtime enviroment is failed for" + ex.getMessage());
        }
    }
}
